package org.zp.handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.zp.pojo.AdminInfo;

@RestController
public class PageHandler {
	// 所有的springmvc的方法中都可以传入request，response，session对象
	@GetMapping("page.act")
	public ModelAndView page(HttpServletRequest req, HttpSession session ,HttpServletResponse resp) {
		String p = req.getParameter("p");
		if(p != null && p.equals("err404")) {
			return new ModelAndView("err404");
		}
		// 如果没有携带参数，则跳转到登录页面
		System.out.println("p的值："+p);
		if(p==null) {
			//请求转发到登录页面
			return new ModelAndView("adminLogin");
		}else {
			AdminInfo admin = (AdminInfo) session.getAttribute("admin");
			if(admin==null) {
				return new ModelAndView("adminLogin");
			}else {
				//请求转发到对应的页面
				return new ModelAndView(p);
			}
		}
		
		
		
		
	}
	// 欢迎页面的使用
			@GetMapping("/")
			public ModelAndView welcomePage(HttpServletRequest req, HttpSession session ,HttpServletResponse resp) {

						return new ModelAndView("login");

			}
	//添加对应的404handler：
			@GetMapping("err404.act")
			public ModelAndView err404() {
				return new ModelAndView("err404");
			}

}
